package com.mrjoew.weekend.services.impl.sys;

import com.mrjoew.weekend.constants.SecurityConstant;
import com.mrjoew.weekend.entities.po.SysSecurityClientPO;
import com.mrjoew.weekend.repositories.sys.SysSecurityClientRepository;
import com.mrjoew.weekend.services.sys.SysSecurityClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author Christopher Jones
 * @ClassName SysSecurityClientServiceImpl
 * @explain
 * @date 2020/6/17 0017 20:59
 */
@Service
public class SysSecurityClientServiceImpl implements SysSecurityClientService, ClientDetailsService {

    @Autowired
    private SysSecurityClientRepository sysSecurityClientRepository;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        Optional<SysSecurityClientPO> sysSecurityClient = sysSecurityClientRepository.findFirstByClientId(clientId);

        if (!sysSecurityClient.isPresent()) {
            throw new ClientRegistrationException(SecurityConstant.CLIENT_ID_NOT_FOUND);
        }

        return sysSecurityClient.get();
    }
}
